﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System;
using System.Linq;
using NativeBuilder;


public static class AndroidBuild
{
    static DateTime startTime;

    public static void Build()
    {
        startTime = DateTime.Now;

        // pre build
        AssetDatabase.Refresh();
        string solutionPath = "NativeBuilderProduct/android_project";

        //check parent dir
        {
            var di = new DirectoryInfo(solutionPath).Parent;
            if (!di.Exists)
            {
                di.Create();
            }
        }

        //if target Path exits, delete it!
        if (Directory.Exists(solutionPath))
        {
            DirectoryInfo dir = new DirectoryInfo(solutionPath);
            dir.Delete(true);
        }

        // build eclipse project
        Debug.Log("Unity building android project...");

        var optionList = BuildOptions.None;
        optionList |= BuildOptions.CompressWithLz4;
        NativeBuilderUtility.Build(solutionPath, UnityEditor.BuildTarget.Android, optionList);


        DateTime endTime = DateTime.Now;
        var useTime = endTime - startTime;
        string msg = "[NativeBuilder] Build Success";
        msg += ", android project release at [" + "NativeBuilderProduct/android_project]";
        msg += ", in " + FormatTime(useTime);
        Debug.Log(msg);
    }

    private static string FormatTime(TimeSpan span)
    {
        return string.Format("{0:F0} munutes {1:F0} seconds", span.TotalMinutes, span.Seconds);
    }
}